<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品名称" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入商品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品编号" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入商品编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店铺名称" prop="id">
        <el-input
          v-model="queryParams.storeName"
          placeholder="请输入店铺名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="marketEnable">
        <el-select v-model="queryParams.marketEnable" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in dict.type.mall_goods_market_enable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="销售模式" prop="salesModel">
        <el-select v-model="queryParams.salesModel" placeholder="请选择销售模式" clearable size="small">
          <el-option
            v-for="dict in dict.type.mall_goods_sales_model"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="商品类型" prop="goodsType">
        <el-select v-model="queryParams.goodsType" placeholder="请选择商品类型" clearable size="small" >
          <el-option
            v-for="dict in dict.type.mall_goods_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="goodsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品名称" align="center" width="250" prop="goodsName" >
        <template slot-scope="scope">
          <div style="margin: 5px 0px; height: 80px; display: flex">
            <div style="">
              <img
                :src="scope.row.original"
                style="height: 60px; margin-top: 1px; width: 60px"
              />
            </div>

            <div style="margin-left: 13px">
              <div class="div-zoom">
                <a  @click="$mall.linkTo(scope.row.id, scope.row.skuId)">{{ scope.row.goodsName }}</a>
              </div>
              <el-popover
                placement="top-start"
                title="扫码在手机中查看"
                width="200"
                trigger="hover"
              >
                <div style="text-align: center">
                  <vue-qr
                    :text="$mall.wapLinkTo(scope.row.id, scope.row.skuId)"
                    :margin="0"
                    colorDark="#000"
                    colorLight="#fff"
                    :size="150"
                  ></vue-qr>
                </div>
                <img
                  slot="reference"
                  src="../../../../assets/qrcode.svg"
                  class="hover-pointer"
                  width="20"
                  height="20"
                  alt=""
                  style="float: left"
                />
              </el-popover>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="商品编号" align="center" prop="id" />
      <el-table-column label="价格" align="center" prop="price" >
        <template slot-scope="scope">
          {{$mall.unitPrice(scope.row.price, '￥')}}
        </template>
      </el-table-column>
      <el-table-column label="销售模式" align="center" prop="newsType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mall_goods_sales_model" :value="scope.row.salesModel"/>
        </template>
      </el-table-column>
      <el-table-column label="商品类型" align="center" prop="goodsType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mall_goods_type" :value="scope.row.goodsType"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="marketEnable" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mall_goods_market_enable" :value="scope.row.marketEnable"/>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="authFlag" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mall_goods_auth_flag" :value="scope.row.authFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="店铺名称" align="center" prop="storeName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-dropdown trigger="click">
            <span class="el-dropdown-link">
              更多<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown" >
              <el-dropdown-item v-hasPermi="['mall-manager:goods:auth']" >
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-top"
                  @click="handleUpper(scope.row)"
                >通过
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item v-hasPermi="['mall-manager:goods:auth']" >
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-bottom"
                  @click="handleDown(scope.row)"
                >拒绝
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item v-hasPermi="['mall-manager:goods:detail']">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleDetail(scope.row)"
                >查看
                </el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { authGoods, getAuthGoodsListData } from "@/api/mall/goods/goods";
import {delAgreement} from "@/api/cms/agreement";

export default {
  name: "goods",
  dicts: ['mall_goods_market_enable','mall_goods_sales_model','mall_goods_type','mall_goods_auth_flag'],

  components: {},
  data() {
    return {
      id: "", //要操作的id
      loading: true, // 表单加载状态
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsName: null,
        id: null,
        storeName: null,
        marketEnable: null,
        salesModel: null,
        goodsType: null,
      },
      goodsAuditForm: {
        // 商品编辑表单
        authFlag: "",
        goodsIds: []
      },
      data: [], // 表单数据
      total: 0, // 表单数据总数
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询商品列表 */
    getList() {
      this.loading = true;
      getAuthGoodsListData(this.queryParams).then(response => {
        this.goodsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /**审核通过*/
    handleUpper(row) {
      const ids = row.id || this.ids
      let idArr = [];
      idArr.push(ids)
      let auditForm = {
        "goodsIds" : idArr,
        "authFlag" : "PASS"
      }
      this.$modal.confirm('是否确认审核通过？').then(function() {
        return authGoods(auditForm);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {});
    },
    /**审核拒绝*/
    handleDown(row) {
      const ids = row.id || this.ids
      let auditForm = {
        "goodsIds" : ids,
        "authFlag" : "REFUSE"
      }
      this.$modal.confirm('是否确认审核拒绝？').then(function() {
        return authGoods(auditForm);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {});
    },
    //查看商品详情
    showDetail(v) {
      let id = v.id;
      this.$router.push({
        name: "goods-detail",
        query: { id: id },
      });
    },
  },
  mounted() {
    this.init();
  },
};
</script>
